$( function () {
    showImage( $( ".attach" ).find( ".attach-name" ), 'attach-img', 'attach-file' );

    $( ".attach-insert" ).click(function(){
        var obj = $( this ).parent();
        var img = obj.find( ".attach-img" ).html();
        var file = img == 1 ? obj.find( ".attach-file" ).html() : obj.find( ".attach-id" ).html();
        addAttach( img, file );
        return false;
    });
    $( ".attach-del" ).click( function () {
        var obj = $( this ).parent();
        var url = del_url + obj.find( '.attach-id' ).html();
        var img = obj.find( ".attach-img" ).html();
        var file = img == 1 ? obj.find( ".attach-file" ).html() : obj.find( ".attach-id" ).html();
        delAttach( obj, url, img, file );
        return false;
    });
    $( ".attach-default" ).click( function () {
        var obj = $( this );
        var id = $( this ).parent().find( '.attach-id' ).html();
        var isdefault = $( this ).attr( 'isDefault' );
        var url = default_url + id + "&v=" + isdefault;
        var text = isdefault == 0 ? '<b style="color: blue;">取消缩略图</b>' : '设置为缩略图';
        $.get( url, function ( result ) {
            if( result == 'success'){
                if( isdefault == 0 ){
                    jBox.tip( "设置缩略图完成!" );
                    obj.attr( 'isDefault', '1' );
                    obj.attr( 'title', '取消缩略图' );
                }else{
                    obj.attr( 'isDefault', '0' );
                    obj.attr( 'title', '设置为缩略图' );
                    jBox.tip( "取消缩略图完成!" );
                }
                obj.html( text );
            }
        });
        return false;
    });
});